package com.wing.cabinet.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CheckedResult;
import com.wing.cabinet.model.response.CheckedResultVO;
import com.wing.cabinet.service.CheckedResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.CheckedResultForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-盘验结果表管理", tags = "盘验结果表管理")
@RestController("appCheckedResultController")
@RequestMapping("cabinet/app/checkedResult")
public class CheckedResultController {

    @Autowired
    private CheckedResultService checkedResultService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询盘验结果列表(盘验记录)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "queryFlag", value = "yes:正常;no:异常", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CheckedResultVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "desc") String order
            ,String queryFlag
            ,String code
    ) {
        Page<CheckedResultVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("queryFlag",queryFlag);
        params.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        params.put("code",code);
        Page<CheckedResultVO> list = checkedResultService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long checkedResultId) {
        CheckedResultVO checkedResultVO = checkedResultService.get4appById(checkedResultId);
        if (checkedResultVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), checkedResultVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CheckedResultForm checkedResultForm) {
        if (checkedResultService.add(checkedResultForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "按柜子查询", notes = "查询")
    @GetMapping("/getCabCodeGroupBy")
    public PageResult<HashMap> getCabCodeGroupBy(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String cabCode
            ,String belongCabinet
    ){
        Page<HashMap> pg = new Page<>(page, limit);

        Page<HashMap> list = checkedResultService.getCabCodeGroupBy(pg,cabCode,belongCabinet);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /*@ApiOperation(value = "按柜子查询待盘验下的不同业务类型的统计数", notes = "查询")
    @GetMapping("/getStatisticsByCabCode")
    public JsonResult getStatisticsByCabCode(String cabCode){
        List<HashMap> map = checkedResultService.getStatisticsByCabCode(cabCode);
        return JsonResult.success(map);
    }*/

    @ApiOperation(value = "不同盘验状态库位分布情况", notes = "查询")
    @GetMapping("/getDetailStatisticsByCabCode")
    public JsonResult getDetailStatisticsByCabCode(String cabCode, Integer spaceCode,Integer businessType){
        List<HashMap> map = checkedResultService.getDetailStatisticsByCabCode(cabCode,spaceCode,businessType);
        return JsonResult.success(map);
    }

    @ApiOperation(value = "按任务查询", notes = "查询")
    @GetMapping("/getTaskGroupBy")
    public PageResult<HashMap> getTaskGroupBy(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,String taskCode
            ,String cabCode
    ){
        Page<HashMap> pg = new Page<>(page, limit);
        IPage<HashMap> map = checkedResultService.getTaskGroupBy(pg,taskCode,cabCode);
        return new PageResult<>(map.getRecords(), map.getTotal());
    }

}
